package com.example.core.result;

import com.example.core.enums.ResultCode;
import lombok.Data;

@Data
public class Res <T> {
    private int code;

    private String msg;

    private T data;

    public static <T> Res<T> ok() {
        return assemble(null, ResultCode.SUCCESS);
    }

    public static <T> Res<T> ok(T data) {
        return assemble(data, ResultCode.SUCCESS);
    }


    public static <T> Res<T> fail() {
        return assemble(null, ResultCode.FAILED);
    }

    public static <T> Res<T> fail(ResultCode resultCode) {
        return assemble(null, resultCode);
    }

    public static <T> Res<T> fail(ResultCode resultCode, String msg) {
        return assemble(resultCode, msg, null);
    }


    private static <T> Res<T> assemble(T data, ResultCode resultCode) {
        Res<T> res = new Res<>();
        res.setMsg(resultCode.getMsg());
        res.setCode(resultCode.getCode());
        res.setData(data);
        return res;
    }

    private static <T> Res<T> assemble(ResultCode resultCode, String msg, T data) {
        Res<T> res = new Res<>();
        res.setMsg(msg);
        res.setCode(resultCode.getCode());
        res.setData(data);
        return res;
    }
}
